﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
// 使用OleDb连接数据库
using System.Data.OleDb;

namespace 实验7_学生成绩管理系统
{
    public partial class FormStudent : FormBase
    {
        string m_StrConn = "Provider=SQLOLEDB;Data Source=.;Integrated Security=SSPI;Initial Catalog=master";
        DBHelper dbhelper = new DBHelper();
        public FormStudent()
            : base("select * from student")
        {
            InitializeComponent();
        }

        private void FormStudent_Load(object sender, EventArgs e)
        {
        }

        // 重写基类方法，将datatable中修改、删除、新增的数据同步到数据库
        protected override void tsb_save_Click(object sender, EventArgs e)
        {
            OleDbConnection conn = new OleDbConnection(m_StrConn);
            OleDbTransaction trans = null;
            try
            {
                conn.Open();
                trans = conn.BeginTransaction();
                OleDbCommand cmd = new OleDbCommand();
                cmd.Connection = conn;
                cmd.Transaction = trans;
                cmd.Parameters.Clear();
                string sql = "delete from student where sno = ?";
                cmd.CommandText = sql;

                DataTable dtSource = GetDateTable();

                //处理删除
                DataTable dtDeleted = dtSource.GetChanges(DataRowState.Deleted);
                if (dtDeleted != null)
                {
                    foreach (DataRow dr in dtDeleted.Rows)
                    {
                        dbhelper.Execute(cmd, dr["sno", DataRowVersion.Original]);
                    }
                }
                //处理更新数据
                sql = "update student set sno = ?, sname = ?, age = ?, dept = ?, sex = ? where sno = ?";
                cmd.CommandText = sql;
                DataTable dtUpdated = dtSource.GetChanges(DataRowState.Modified);
                if (dtUpdated != null)
                {
                    foreach (DataRow dr in dtUpdated.Rows)
                    {
                        dbhelper.Execute(cmd, dr["sno"], dr["sname"], dr["age"], dr["dept"], dr["sex"], dr["sno", DataRowVersion.Original]);
                    }
                }
                //处理插入数据
                sql = "insert into student (sname, sno, age, dept, sex) values (?, ?, ?, ?, ?)";
                DataTable dtInserted = dtSource.GetChanges(DataRowState.Added);
                cmd.CommandText = sql;
                if (dtInserted != null)
                {
                    foreach (DataRow dr in dtInserted.Rows)
                    {
                        dbhelper.Execute(cmd, dr["sname"], dr["sno"], dr["age"], dr["dept"], dr["sex"]);
                    }
                }
                trans.Commit();
                dtSource.AcceptChanges();
                MessageBox.Show("保存成功");
            }
            catch (Exception ex)
            {
                if (trans != null)
                    trans.Rollback();
                throw ex;
            }
            finally
            {
                conn.Close();
            }
        }
        // 重写基类模糊查询方法
        protected override void btn_selectByKey_Click(object sender, EventArgs e)
        {
            string key = GetSelectKey();
            DataTable dt = dbhelper.GetDataTable("select * from student where CHARINDEX(?, SNO+SNAME+SEX+DEPT+CONVERT(char, AGE)) > 0", null, key);
            RefreshView(dt);
            // 输出操作记录
            AddRecord("查询：" + key);
        }

    }
}
